package com.cheetah.start.module.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.solon.plugins.pagination.Page;
import com.cheetah.start.common.base.query.PageQuery;
import com.cheetah.start.common.base.result.Result;
import com.cheetah.start.common.base.result.ResultController;
import com.cheetah.start.common.base.result.ResultTable;
import com.cheetah.start.common.enums.BusinessTypeEnum;
import com.cheetah.start.module.system.domain.SysDictData;
import com.cheetah.start.module.system.service.SysDictDataService;
import lombok.extern.slf4j.Slf4j;
import org.noear.solon.annotation.*;
import org.noear.solon.core.handle.ModelAndView;
import org.noear.solon.validation.annotation.NotBlank;
import org.noear.solon.validation.annotation.Valid;
import org.noear.solon.validation.annotation.Validated;

import java.util.List;

/**
 * @author Feel
 * @create 2021/7/18
 **/
@Slf4j
@Valid
@Controller
@Mapping("/dictData")
public class SysDictDataController extends ResultController {

    @Inject
    SysDictDataService dictDataService;


    /**
     * 编辑页面
     *
     * @param dictDataId dict数据id
     * @return {@link ModelAndView}
     */
    @Get
    @Mapping("/edit")
    public ModelAndView edit(String dictDataId) {
        SysDictData dictData = dictDataService.getById(dictDataId);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.put("sysDictData", dictData);
        modelAndView.view("/dict/data/edit");
        return modelAndView;
    }


    /**
     * 启用
     *
     * @param sysDictData sys dict 类型数据
     * @return {@link Result}
     */
    @SaCheckPermission("sys:dictData:switch")
    @Post
    @Mapping("/enable")
    public Result enable(@Body SysDictData sysDictData) {
        SysDictData byId = dictDataService.getById(sysDictData.getDictDataId());
        byId.setEnable("0");
        return decide(dictDataService.updateById(byId), BusinessTypeEnum.EDIT);
    }


    /**
     * 禁用
     *
     * @param sysDictData sys dict类型数据
     * @return {@link Result}
     */
    @Post
    @Mapping("/disable")
    public Result disable(@Body SysDictData sysDictData) {
        SysDictData byId = dictDataService.getById(sysDictData.getDictDataId());
        byId.setEnable("1");
        return decide(dictDataService.updateById(byId), BusinessTypeEnum.EDIT);
    }

    /**
     * 通过代码
     *
     * @param code 代码
     * @return {@link Result}
     */
    @Get
    @Mapping("/getByCode")
    public Result getByCode(String code) {
        List<SysDictData> byDictCode = dictDataService.getByDictCode(code);
        return success(byDictCode);
    }

    /**
     * 数据
     *
     * @param pageQuery 页面查询
     * @param typeCode  类型代码
     * @return {@link ResultTable}
     */
    @SaCheckPermission("sys:dictData:data")
    @Get
    @Mapping("/data")
    public ResultTable data(PageQuery pageQuery, String typeCode) {
        Page<SysDictData> page = new Page<>(pageQuery.getPage(), pageQuery.getLimit());
        QueryWrapper<SysDictData> wrapper = new QueryWrapper<>();
        wrapper.eq("type_code", typeCode);
        dictDataService.page(page, wrapper);
        return pageTable(page.getRecords(), page.getTotal());
    }


    /**
     * 添加
     *
     * @param sysDictData sys dict类型数据
     * @return {@link Result}
     */
    @SaCheckPermission("sys:dictData:add")
    @Post
    @Mapping("/add")
    public Result add(@Validated @Body SysDictData sysDictData) {
        return decide(dictDataService.save(sysDictData), BusinessTypeEnum.ADD);
    }


    /**
     * 更新
     *
     * @param sysDictData sys dict类型数据
     * @return {@link Result}
     */
    @SaCheckPermission("sys:dictData:edit")
    @Post
    @Mapping("/update")
    public Result update(@Validated @Body SysDictData sysDictData) {
        return decide(dictDataService.updateById(sysDictData), BusinessTypeEnum.EDIT);
    }


    /**
     * 删除
     *
     * @param dictDataId dict 数据 id
     * @return {@link Result}
     */
    @SaCheckPermission("sys:dictData:delete")
    @Post
    @Mapping("/remove/{dictDataId}")
    public Result remove(@PathVar @NotBlank(message = "不能为空") String dictDataId) {
        return decide(dictDataService.removeById(dictDataId), BusinessTypeEnum.REMOVE);
    }


}
